package com.caiyouxi.app.service.impl;

import com.caiyouxi.app.mapper.AppConfigMapper;
import com.caiyouxi.app.model.AppConfigBean;
import com.caiyouxi.app.service.AppConfigService;
import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.cache.RedisService;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.util.UGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AppConfigServiceImpl extends BaseService implements AppConfigService{
    @Autowired
    private AppConfigMapper mapper;
    @Autowired
    private RedisService redisService;

    @Override
    public ResData delete_appConfig(AppConfigBean bean) {
        return success(mapper.delete_appConfig(bean.getApp_id()));
    }

    @Override
    public ResData insert_appConfig(AppConfigBean bean) {
        if(bean.getCreate_time() == null){
            bean.setCreate_time(new Date());
        }
        if(bean.getUpdate_time() == null){
        bean.setUpdate_time(new Date());}
        //设置cp_app_key
        Integer info = bean.getApp_id();
        String appkey = UGenerator.generate_app_key(info);
        bean.setCp_app_key(appkey);
        //设置cp_app_secret
        bean.setCp_app_secret(UGenerator.generate_app_secret());
        return success(mapper.insert_appConfig(bean));
    }

    @Override
    public ResData update_appConfig(AppConfigBean bean) {
        if(bean.getUpdate_time() == null){
            bean.setUpdate_time(new Date());
        }
        return success(mapper.update_appConfig(bean));
    }

    @Override
    public ResData select_appconfig_list(AppConfigBean bean) {
        Integer page_list = Integer.parseInt(redisService.get("sys.pagesize").toString());

        Integer pageSize = 0;
        if(bean.getPage_size() == 0){
            pageSize = page_list;
        }
        QueryParams pageList = new QueryParams();
        //设置pageList
        bean.setPage_size(pageSize);
        pageList.setPage_size(bean.getPage_size());
        pageList.setPage(bean.getPage());
        pageList.setList(mapper.get_all_appConfig(bean));
        pageList.setTotal(mapper.get_count_appConfig(bean));
        return success(pageList);
    }
}
